<%@page import="com.ym.b2b.cache.configcache.WebAppConfig"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<div class="easyui-layout" style="width:100%;height:100%;" fit="true">
    <div data-options="region:'center',title:'',split:true" style="height:100%;">
		<form id="dictionaryInfoForm" method="post" >
		<input type="hidden" id="dictionaryId" name="dictionaryId" data-options="required:true">
			<table class="detailTable">
			<tr>
                <th>分类:</th>
                <td>
                	<select id="keyCatagory" name="keyCatagory" data-options="required:true">
                		<option value="">请选择</option>
                		<option value="文字">文字字典</option>
                		<option value="图片">图片字典</option>
                		<option value="超连接">超连接字典</option>
                	</select>
                
                </td>
            </tr> 
            <tr>
                <th>标题:</th>
                <td><input class="easyui-validatebox" id="title" name="title" style="width:300px;" data-options="required:true">(如:客服A)</td>
            </tr>
    	 	<tr>
                <th>键:</th> 
                <td>
                   <input class="easyui-validatebox " id="key" name="key"   data-options="required:true" >(如:SERVICE-A)
                  
                </td>
            </tr>
            <tr>
                <th>值:</th>
                <td><input class="easyui-validatebox " id="value" name="value" style="width:600px;" >
                    
                    <input type="button"  class="button" id="chooseImg" onclick="chooseImga();" value="选择"   >
                </td>
            </tr>
            
              <tr>
                <th>备注:</th>
                <td>
                <textarea rows="5" cols="70" id="notes" name="notes">
                
                </textarea>
                </td>
            </tr>
            
             <tr>
                <th>是否启用:</th>
                <td>
                    <input type="radio" id="status" name="status" value="0" checked="checked">否
				    <input type="radio" id="status" name="status" value="1" >是
                </td>
            </tr>
            
            <tr>
			    <th>操作：</th>
				<td align="center" > 
					<input type="button" onclick="submitForm()" class="button" value="提交" >
					<input type="button" onclick="pms.resetForm('dictionaryInfoForm')" class="button" value="重置" >
				  <!--   <input type="button" onclick="test()" class="button" value="测试" >  -->         
				</td>
			</tr>
			
		</table>
		</form>
    </div>
</div>  
<script>
//标示 0：save方法，1：update方法
var flag =0; 
$(function(){
	var dictionaryId="<%=request.getParameter("code")%>";
	if(dictionaryId!="null"){	//save	
		flag = 1;
		var param = {"dictionaryId":dictionaryId};
 		$.pmsAjax({
			url:'<c:url value="/dictionary/searchEmSDictionary.do"/>',
			data:param,
			success:function(data){
				console.log(data);
				var emSDictionary = data.data.dictionary;	
				if(data.code=="200"){
					//alertShow("成功");
					$('#dictionaryInfoForm').form('load', emSDictionary);
					$("input[name='key']").attr("readonly","readonly");
				}else{
					alertError(data.result);
				}	
			}
		});
	}

	$("#keyCatagory").change(function(){
		if($(this).val()=="图片"){
			$("#chooseImg").show();
		}
	});
});
function submitForm(){
	if(flag == 0){
		doInsert();
	}else{
		doUpdate();
	}	
}
//insert
function doInsert(){
	$('#dictionaryInfoForm').form('submit', {
		url:'<c:url value="/dictionary/insertDictionary.do"/>',
	    onSubmit: function(param){
	    	if(!$("#dictionaryInfoForm").form('validate')){
	    		return false;
	    	}
	    }, 
	    success:function(data){
		    var ret = eval("("+data+")");
	     	if(ret.code=='200'){
	     		parent.alertShow("新增成功");
	     		parent.dictionaryTable.refresh();
	     		parent.$("#w_dictionary_detail").dialog('close');
		    }else{
		    	parent.alertError(ret.result);
			}
	    }
	});
}
//update
function doUpdate(){
	$('#dictionaryInfoForm').form('submit', {
	    url:'<c:url value="/dictionary/updateEmSDictionary.do"/>',
	    onSubmit: function(param){
	    	if(!$("#dictionaryInfoForm").form('validate')){
	    		return false;
	    	}
	    }, 
	    success:function(data){
		    var ret = eval("("+data+")");
	     	if(ret.code=='200'){
	     		parent.alertShow("修改成功");
	     		parent.initDictionary();
	     		parent.$("#w_dictionary_detail").dialog('close');
	     		
		    }else{
		    	alertError(ret.result);
			}
	    }
	});
}
//choose  广告图
function chooseImga(){
	$.pmsDialog({
		id:"plug_upload",  //固定ID
		title:'上传控件',
		isIf:'true',
		params:{    //这4个必须填   可
    		"fileType":"img", //必填项  file  or   img or all
    		"fileExtensions":"", //可选值 可指定后缀 即上传类型
    		"oneSelect":'true',
    		"moduleId":"img",
    		"funcId":"ad",
    		"dataId":"ad"
    	},
		method:'post',
		href:'<c:url value="/views/jsp/upload/com_uploader_s.jsp"/>',
		width:900,
		height:464
	});
}
function getFiles(obj){
	if(obj.length>0){
		$("#value").val('<%=WebAppConfig.getPicPath()%>'+obj[0].fileWebPath);
		 console.log(obj);
	}
}
function test(){
	$("#key").val("ADVERTISE_1");
	$("#value").val("明星广告");
	$("#keyCatagory").val("广告字典");
	$("#title").val("平面广告");
}

</script>
